//
//  HYFeedBackViewController.m
//  HaiYue
//
//  Created by skyline on 2018/1/19.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYFeedBackViewController.h"
@interface HYFeedBackViewController ()
@property (nonatomic, strong) HYTextView *textView;
@property (nonatomic, strong) HYButton *button;
@property (nonatomic, copy) NSString *buttonTitle;
@property (nonatomic, copy) NSString *api;
@end

@implementation HYFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [self.title length] > 1?self.title:@"意见反馈";
    self.view.backgroundColor = LINE_COLOR;
    // Do any additional setup after loading the view.
}

- (HYTextView *)textView
{
    if (!_textView)
    {
        _textView = [[HYTextView  alloc] init];
        _textView.placholder = @"请提出您的意见与建议，管理员回复后会在消息中心通知您";
        [self.view addSubview:_textView];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(GAP);
            make.left.equalTo(self.view).with.offset(GAP);
            make.right.equalTo(self.view).with.offset(-GAP);
            make.height.mas_equalTo(SCREEN_HEIGHT / 4);
        }];
    }
    return _textView;
}

- (HYButton *)button
{
    if (!_button)
    {
        _button = [[HYButton alloc] initWithTitle:self.buttonTitle backColor:THEME_COLOR];
        _button.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightLight];
        [_button addTarget:self action:@selector(actionForFeedBackBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textView.mas_bottom).with.offset(GAP);
            make.left.equalTo(self.textView);
            make.right.equalTo(self.textView);
            make.height.mas_equalTo(ButtonHeight);
        }];
    }
    return _button;
}

- (void)actionForFeedBackBtn
{
    if ([_textView.text length] < 1)
    {
        [CCAlert showMessage:@"内容不能为空" hidden:nil];
        return;
    }
    [_textView endEditing:YES];
    [self startHUD];
    
    NSDictionary *para = @{@"text":self.textView.text,
                           @"gameid":[self.gameid length] > 1 ?self.gameid:@"",
                           @"articleid":[self.articleid length] > 1?self.articleid:@""};
    [CCHttp post:self.api para:para success:^(id  _Nonnull response) {
        [self endHUD];
        int retcode = [[response valueForKey:@"retcode"] intValue];
        [CCAlert showMessage:@"提交成功" hidden:^{
            if (retcode == 100)
            {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
        [CCAlert showMessage:error.localizedDescription hidden:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
//    [HYHttps post:self.api para:para complete:^(id response) {
//        [self endHUD];
//        int retcode = [[response valueForKey:@"retcode"] intValue];
//        [CCAlert showMessage:@"提交成功" hidden:^{
//            if (retcode == 100)
//            {
//                [self.navigationController popViewControllerAnimated:YES];
//            }
//        }];
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//        [CCAlert showMessage:[dict valueForKey:@"msg"] hidden:^{
//            [self.navigationController popViewControllerAnimated:YES];
//        }];
//    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textView endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.style = HYViewNaviBarStyleOnlyLeftBtn;
    self.button.hidden = NO;
    //    [self.textView becomeFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
